<?php


namespace App\Http\Controllers\Mp;


use App\Http\Controllers\Controller;
use App\Http\Resources\HousePaginationCollection;
use App\Http\Resources\PaginationCollection;
use App\Model\Repair;
use App\Utils\Result;
use Illuminate\Http\Request;

class RepairController extends Controller
{
    public function index(Request $request)
    {
        // Get the current page number from the request parameters (default to 1)
        $page = $request->input('page', 1);

        // Get the number of houses to show per page
        $perPage = 20;

        $query = Repair::query()->where('business_id', $this->getBusinessId());

        $data = $query->with('tenant', 'house')
            ->whereHas('house')
            ->orderByDesc('id')
            ->paginate($perPage, ['*'], 'page', $page);

        return Result::success(new PaginationCollection($data));
    }

    public function detail($id)
    {
        $data = Repair::query()->with(['house', 'contract'])->find($id);
        return Result::success($data);
    }

    public function confirm($id)
    {
        $res = Repair::find($id);
        $res->status = 2;
        $res->save();

        return Result::success($res);
    }
}
